package com.javasm.crm.service.impl.productimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.criteria.GoodsCriteria;
import com.javasm.crm.domin.entity.productEntity.GoodsVo;
import com.javasm.crm.domin.entity.productEntity.ProductVo;
import com.javasm.crm.mapper.goodsmapper.GoodsVoMapper;
import com.javasm.crm.mapper.goodsmapper.ProductVoMapper;
import com.javasm.crm.service.productservice.GoodsVoService;
import com.javasm.crm.utils.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @Author：zhangyang
 * @Version：1.0
 * @Date：2021/7/14-10:23
 * @desp:
 * @since:
 */
@Service
@Transactional
public class GoodsVoServiceImpl extends ServiceImpl<GoodsVoMapper, GoodsVo> implements GoodsVoService {
    @Autowired
    GoodsVoMapper goodsVoMapper;
    @Override
    public PageResult<GoodsVo> searchPageByCriteria(GoodsCriteria goodsCriteria) {
        Integer currentPage = goodsCriteria.getCurrentPage();

        Integer pageSize = goodsCriteria.getPageSize();
        if (StringUtils.isEmpty(currentPage)) {
            currentPage=1;
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize=5;
        }

        QueryWrapper<GoodsVo> exampleQueryWrapper = new QueryWrapper<>();
        exampleQueryWrapper.eq("g.deleted",0);
        if (!StringUtils.isEmpty(goodsCriteria.getGoodName())){
            exampleQueryWrapper.like("g.good_name",goodsCriteria.getGoodName());
        }
        if (!StringUtils.isEmpty(goodsCriteria.getProductModel())) {
            exampleQueryWrapper.like("p.product_model", goodsCriteria.getProductModel());
        }
        if (!StringUtils.isEmpty(goodsCriteria.getBrandId())) {
            exampleQueryWrapper.eq("pb.brand_id", goodsCriteria.getBrandId());
        }
        if (!StringUtils.isEmpty(goodsCriteria.getColorId())) {
            exampleQueryWrapper.eq("pc.color_id", goodsCriteria.getColorId());
        }
        if (!StringUtils.isEmpty(goodsCriteria.getTypeId())) {
            exampleQueryWrapper.eq("pt.type_id", goodsCriteria.getTypeId());
        }
        Page<GoodsVo> examplePage = new Page<>(currentPage, pageSize);
        List<GoodsVo> queryall = goodsVoMapper.queryall(examplePage, exampleQueryWrapper);

        return new PageResult<GoodsVo>(examplePage.getTotal(), queryall);
    }

    @Override
    public int deleteById(Serializable goodId) {
        return goodsVoMapper.deleteById(goodId);
    }

    @Override
    public GoodsVo findById(Serializable goodId) {
        return goodsVoMapper.selectById(goodId);
    }
}
